﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Utilities;
using ViCode_LeVi.Controls.MessageBoxs;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabControl_VMQuestionAnswer.xaml
    /// </summary>
    public partial class VMTabControl_VMQuestionAnswer : UserControl, IVMRefresh
    {
        public VMTabControl_VMQuestionAnswer()
        {
            InitializeComponent();
            new ViCode_LeVi.Utilities.PropertyChangeNotifier(this, FrameworkElement.DataContextProperty, (s, e) =>
                {
                    TabControl_QuestionAnswer_CreateTabItem();
                });
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(VMTabControl_VMQuestionAnswer_DataContextChanged);
            //this.Loaded += new RoutedEventHandler(VMTabControl_VMQuestionAnswer_Loaded);
        }

        void VMTabControl_VMQuestionAnswer_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            TabControl_QuestionAnswer_CreateTabItem();
        }

        void VMTabControl_VMQuestionAnswer_Loaded(object sender, RoutedEventArgs e)
        {
            TabControl_QuestionAnswer_CreateTabItem();
        }

        /// <summary>
        /// Cần 1 tab default de add them thong tin
        /// </summary>
        public void TabControl_QuestionAnswer_CreateTabItem()
        {
            var stepInfo = (Step_QuestionAnwserStep)this.DataContext;
            Step_QuestionAnwserGroup[] data = stepInfo == null ? null : stepInfo.QuestionAnwserGroups_Source.ToArray();
            if (data == null || data.Length == 0)
            {
                TabControl_AnswerQuestion.Items.Clear();
            }
            else
            {
                TabControl_AnswerQuestion.Items.Clear();
                for (int i = 0; i < data.Length; i++)
                {
                    Step_QuestionAnwserGroup d = data[i];
                    TabItem item = new TabItem();
                    item.SetBinding(FrameworkElement.DataContextProperty, new Binding("DataContext.QuestionAnwserGroups_Source[" + i.ToString() + "]")
                    {
                        RelativeSource = new RelativeSource
                            (RelativeSourceMode.FindAncestor
                            , typeof(VMTabControl_VMQuestionAnswer)
                            , 1)
                    });
                    TabControl_AnswerQuestion.Items.Add(item);

                    //Header
                    VMTextBlockExt header = new VMTextBlockExt() { Margin= new Thickness(2,5,2,3 )};
                    //header.SetBinding(VMTextBlock.TextProperty, new Binding("Name"));
                    header.TextPath = "Name";

                    item.Header = header;

                    //VMQuestionAnswer
                    VMQuestionAnswer vmqa = new VMQuestionAnswer()
                    {
                        HorizontalAlignment = HorizontalAlignment.Stretch
                        ,
                        VerticalAlignment = VerticalAlignment.Stretch
                    };
                    item.Content = new ScrollViewer() { Content = vmqa, HorizontalScrollBarVisibility= ScrollBarVisibility.Disabled };                    
                    vmqa.Action_Remove_QA = (dataRemove) =>
                        {                            
                            Window_Ask_Delete_Question w = new Window_Ask_Delete_Question(d, dataRemove, TabControl_QuestionAnswer_CreateTabItem) 
                            {
                            };
                            w.ShowDialog();
                            //if (MessageBox.Show("Do you want to delete this QA?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
                            //{
                            //    d.QuestionAnswers_Source.Remove(dataRemove);
                            //    TabControl_QuestionAnswer_CreateTabItem();
                            //}
                        };
                    vmqa.Action_AddNew_QA = () =>
                        {
                            d.QuestionAnswers_Source.Add(new Step_QuestionAnwser() { Question = string.Empty, Answer = string.Empty });
                            TabControl_QuestionAnswer_CreateTabItem();
                        };
                    vmqa.SetBinding(FrameworkElement.DataContextProperty, new Binding("QuestionAnswers_Source"));
                }
                TabControl_AnswerQuestion.UpdateLayout();
                this.UpdateLayout();
            }


        }

        #region IVMRefresh Members

        public void VMRefresh()
        {
            for (int i = 0; i < TabControl_AnswerQuestion.Items.Count; i++)
            {
                var tabItem = TabControl_AnswerQuestion.Items[i] as TabItem;
                var checkBinding = tabItem.Content as VMQuestionAnswer;
                UpdateBindingDataContext.UpdateDataContext(checkBinding);
            }
        }

        #endregion
    }
}
